package appapi.service.impl;

import appapi.Request.AlarmPageReq;
import appapi.Vo.brider.AlarmHandleVO;
import appapi.Vo.brider.AlarmVO;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.AlarmHandle;
import appapi.entity.bridge.DeviceAlarm;
import appapi.mapper.AlarmHandleMapper;
import appapi.mapper.AlarmMapper;
import appapi.service.AlarmService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author hlm
 * @date
 * @description: 设备告警记录
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, DeviceAlarm> implements AlarmService {

    @Resource
    private AlarmMapper alarmMapper;

    @Resource
    private AlarmHandleMapper alarmHandleMapper;

    /**
     * 分页查询设备告警记录列表
     *
     * @return
     */
    @Override
    public IPage<AlarmVO> getAlarmByPage(AlarmPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<AlarmVO> alarmVOList = alarmMapper.getAlarmByPage(req);
        PageInfo<AlarmVO> info = new PageInfo<>(alarmVOList);
        IPage<AlarmVO> alarmVOIPage = new Page<>();
        if(alarmVOList.size() > 0){
            alarmVOIPage.setRecords(alarmVOList);
            alarmVOIPage.setSize(req.getPageSize());
            alarmVOIPage.setTotal(info.getTotal());
            alarmVOIPage.setCurrent(req.getPageNum());
        }
        return alarmVOIPage;
    }

    /**
     * 查询设备告警记录列表
     *
     * @return
     */
    @Override
    public List<AlarmVO> getAlarmList(AlarmPageReq req) {
        List<AlarmVO> alarmVOList = alarmMapper.getAlarmByPage(req);
        return alarmVOList;
    }


    /**
     * 删除
     *
     * @return
     */
    @Override
    public void delAlarm(Long id) {
        alarmMapper.deleteById(id);
    }


    /**
     * 告警自动恢复
     *
     * @return
     */
    @Override
    public Integer autoRestoreAlarm(DeviceAlarm entity) {
        Integer iRet = 0;
        Long id =alarmMapper.selectIdByDetail(entity);
        if(id!=null)
        {
            entity.setId(id);
            iRet = alarmMapper.autoRestoreAlarm(entity);
        }
        return iRet;
    }
    /**
     * 手动恢复/处置
     *
     * @return
     */
    @Override
    public void updateAlarm(AlarmHandleVO alarmHandleVO , UserInfo userInfo ){
        //生成设备报警手动处理记录
        AlarmHandle alarmHandle = BeanCopyUtils.copyBean(alarmHandleVO , AlarmHandle.class);
        alarmHandle.setHandleTime(LocalDateTime.now());
        alarmHandle.setCreateTime(LocalDateTime.now());
        alarmHandle.setCreateUser(userInfo.getUsername());
        alarmHandleMapper.insert(alarmHandle);
        //更新设备报警记录
        DeviceAlarm deviceAlarm = alarmMapper.selectById(alarmHandleVO.getId());
        if(deviceAlarm != null){
            deviceAlarm.setRestoreTime(LocalDateTime.now());
            deviceAlarm.setHandleTime(LocalDateTime.now());
            deviceAlarm.setAlarmRestoreType(1);
            if(alarmHandleVO.getHandleType() == 0){
                deviceAlarm.setAlarmStatus(1);
            }
            if(alarmHandleVO.getHandleType() == 1){
                deviceAlarm.setAlarmStatus(2);
            }
            alarmMapper.updateById(deviceAlarm);
        }
    }

}
